<template>
  <el-form ref="userForm" v-model="userForm">
    <el-form-item label="密码">
      <el-input v-model.trim="userForm.password" type="password" show-password />
    </el-form-item>
    <el-form-item label="确认密码">
      <el-input v-model.trim="userForm.confirmPass" type="password" show-password />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { update } from '@/api/user'
export default {
  data() {
    return {
      userForm: {
        password: '',
        confirmPass: ''
      }
      // userFormRules: {
      //   password: [{ required: true, validator: checkPass, trigger: 'blur' }],
      //   confirmPass: [{ required: true, validator: checkPass2, trigger: 'blur' }]
      // }
    }
  },
  methods: {
    submit() {
      if (this.checkForm()) {
        // 验证成功
        const adminQo = {
          id: this.$store.getters.user.id,
          password: this.userForm.password
        }
        update(adminQo).then(res => {
          this.$message.success('修改成功,需要重新登陆')
          this.$store.dispatch('user/logout').then(res => {
            setTimeout(() => {
              this.$router.push('/login')
            }, 1500)
          })
        })
      }
    },
    checkForm() {
      if (this.userForm.password === '') {
        this.$message.error('请输入密码')
        return false
      } else if (this.userForm.confirmPass === '') {
        this.$message.error('请输入确认密码')
        return false
      } else if (this.userForm.password !== this.userForm.confirmPass) {
        this.$message.error('两次密码不一致')
        return false
      }
      return true
    }
  }
}
</script>
